package com.qianyu.project.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.bean.Form;
import com.qianyu.project.core.bean.TargetTable;
import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.core.mapper.FormMapper;
import com.qianyu.project.core.service.IFormService;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author gs
 * @date create in 2020/12/11 16:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Validated
public class FormServiceImpl implements IFormService {

    @Autowired
    private FormMapper formMapper;
    @Autowired
    private FlowUserProvider flowUserProvider;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Form form){
        System.out.println(flowUserProvider.getFlowUser().getId());
        System.out.println(form);
        form.setEid(flowUserProvider.getFlowUser().getId());


        formMapper.save(form);

    }

    @Override
    public String renderPrimaryKeyColumn(@NotNull String target) {
        return formMapper.selectPrimaryKeyColumn(target);
    }

    @Override
    public Map<String, Object> renderTarget(@NotNull Integer fid) {
        Form form = formMapper.selectById(fid);
        String primaryKey = formMapper.selectPrimaryKeyColumn(form.getTarget());
        TargetTable targetTable = new TargetTable();
        targetTable.setColumn(primaryKey);
        targetTable.setId(form.getId());
        targetTable.setTable(form.getTarget());
        return formMapper.selectTarget(targetTable);
    }

    @Override
    public List<Form> getByUserRole() {
        return formMapper.selectByRole(flowUserProvider.getFlowUser().getUserRoles());
    }

    @Override
    public List<Form> getByUser() {
        return formMapper.selectByUser(flowUserProvider.getFlowUser().getId());
    }

    @Override
    public void update(Form form) {
        formMapper.update(form);
    }

    @Override
    public IPage<Form> selectMybacklog(Map<String,String> params) {
        IPage page = PageUtil.createPage(params);
        params.put("id",flowUserProvider.getFlowUser().getId().intValue()+"");
        return formMapper.selectMybacklog(page,params);
    }

    @Override
    public List<Form> zhuyedeshenqing() {
        Long id = flowUserProvider.getFlowUser().getId();
        return formMapper.zhuyedeshenqing(id);
    }

    @Override
    public void removeById(String fid) {
        formMapper.deleteById(fid);
    }

    @Override
    public Form getById(@NotNull Integer fid) {
        return formMapper.selectById(fid);
    }
}
